// 根据屏幕尺寸判断是否为移动端
export const isMobile = () => {
    return window.innerWidth <= 1200 ? true : false
}
/**
 * 金额保留小数点多少位
 * @param {number | string} val 金额
 * @param {number} num  保留的位数
 * @returns 
 */
export const numberFormat = (val,num = 6) => {
	let newVal = val +''
	try {
		// 1.最多显示6位小数(不四舍五入: 1.6758796 -> 1.675879)
		// 2.小数点后面多余的0去除(1.230000 -> 1.23)
		// 3.整数后面显示.00
		// 先保留6位
		let numberVal = newVal.substring(0,newVal.indexOf(".") + (num + 1))
		numberVal = Number(numberVal)
		// 判断是否为整数
		if(numberVal % 1 === 0) {
			numberVal = numberVal.toFixed(2)
		}
		// 判断是否只有一位小数
		if (newVal.indexOf('.') !== -1) {
			// 分割整数部分和小数部分
			let sVal = numberVal + ''
			let parts = sVal.split('.');
			if(parts[1].length === 1) {
				numberVal = numberVal.toFixed(2)
			}
		}
		newVal = numberVal
	} catch (error) {
		console.log('数字转化错误:',error) 
	}
	return newVal
}
/**
 * 根据涨幅字段判断是否为涨
 * @param {number | string} value 
 */
export const isUp = (value = '') => {
  value = value + ''
	if(value && value.indexOf('+') > -1) return true
	return false
}

/**
 * 函数防抖 (只执行最后一次点击)
 * @param fn
 * @param delay
 * @returns {Function}
 * @constructor
 */
export const debounce = (fn, t) => {
  const delay = t || 500
  let timer
  return function() {
    const args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      timer = null
      fn.apply(this, args)
    }, delay)
  }
}
/**
 * 函数节流
 * @param fn
 * @param interval
 * @returns {Function}
 * @constructor
 */
export const throttle = (fn, t) => {
  let last
  let timer
  const interval = t || 500
  return function() {
    const args = arguments
    const now = +new Date()
    if (last && now - last < interval) {
      clearTimeout(timer)
      timer = setTimeout(() => {
        last = now
        fn.apply(this, args)
      }, interval)
    } else {
      last = now
      fn.apply(this, args)
    }
  }
}